package controller

import (
	"agent-list---web-development/list/common"
	"agent-list---web-development/list/model"
	"agent-list---web-development/list/response"
	"agent-list---web-development/list/util"
	"log"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// CreateTask 创建
func CreateTask(ctx *gin.Context) {
	task := util.GetTask(ctx)
	DB := common.DB
	task1 := model.Task{}
	DB.Where("account=? AND title=? AND starttime=? AND endtime=? AND level=? AND status=?", task.Account, task.Title, task.StartTime, task.EndTime, task.Level, task.Status).First(&task1)
	if task1.Id != 0 {
		response.Response(ctx, http.StatusUnprocessableEntity, 422, nil, "该代办已存在！")
		return
	}
	err := DB.Create(task).Error
	if err != nil {
		log.Println("err:", err)
		response.Response(ctx, http.StatusBadRequest, 422, nil, "创建失败，请重试！")
		return
	}
	response.Success(ctx, gin.H{"taskid": task.Id}, "创建成功!")
}

// EditTask 编辑
func EditTask(ctx *gin.Context) {
	DB := common.GetDB()
	newTask := util.GetTask(ctx)
	taskIDStr := ctx.PostForm("taskid")
	taskID, err := strconv.Atoi(taskIDStr)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "获取代办错误，请重试！")
		return
	}
	var task model.Task
	if err := DB.Where("id = ?", taskID).First(&task).Error; err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "找不到该代办!")
		println("err:", err)
		return
	}
	task.Title = newTask.Title
	task.Status = 0
	task.StartTime = newTask.StartTime
	task.EndTime = newTask.EndTime
	task.Level = newTask.Level

	if err := DB.Save(&task).Error; err != nil {
		response.Response(ctx, http.StatusInternalServerError, 500, nil, "编辑代办失败！")
		println("err:", err)
		return
	}

	response.Success(ctx, nil, "编辑成功！")
}

// DeleteTask 删除
func DeleteTask(ctx *gin.Context) {
	DB := common.GetDB()
	taskIDStr := ctx.PostForm("taskid")
	taskID, err := strconv.Atoi(taskIDStr)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "id解析发生错误！")
		return
	}
	var task model.Task
	if err := DB.Where("id = ?", taskID).First(&task).Error; err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "找不到该代办！")
		println("err:", err)
		return
	}
	err1 := DB.Delete(task).Error
	if err1 != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "删除失败！")
		println("err:", err1)
		return
	}
	response.Success(ctx, nil, "删除成功！")
}

// CompleteTask 完成
func CompleteTask(ctx *gin.Context) {
	DB := common.DB
	taskID := ctx.PostForm("taskid")
	var task model.Task
	if err := DB.Where("id = ?", taskID).First(&task).Error; err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "找不到该代办！")
		println("err:", err)
		return
	}
	err := DB.Exec("UPDATE tasks SET status=? WHERE id=?", 1, taskID).Error
	if err != nil {
		log.Println("err:", err)
		response.Response(ctx, http.StatusBadRequest, 422, nil, "操作失败，请重试！")
		return
	}
	response.Success(ctx, nil, "操作成功！")
}

// TaskSummary 代办总结
func TaskSummary(ctx *gin.Context) {
	DB := common.DB
	taskID := ctx.PostForm("taskid")
	summary := ctx.PostForm("summary")
	var task model.Task
	if err := DB.Where("id = ?", taskID).First(&task).Error; err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "找不到该代办！")
		println("err:", err)
		return
	}
	err := DB.Exec("UPDATE tasks SET summary=? WHERE id=?", summary, taskID).Error
	if err != nil {
		log.Println("err:", err)
		response.Response(ctx, http.StatusBadRequest, 422, nil, "操作失败，请重试！")
		return
	}
	response.Success(ctx, nil, "操作成功")
}
